<script setup>
import treeChildren from './tree-chilren.vue'
import {defineEmits, defineProps, ref} from "vue";
const props = defineProps({
  treeData: {
    type: Object,
    default: () => {
      return {}
    }
  },
})

let showChild = ref(false)

const emit = defineEmits(['getId'])

const getIdFun = (obj) => {
  emit('getId', obj)
}

const showOrHidChild = () => {
  showChild.value = !showChild.value
}

const mergeFun = (id) => {
  emit('getId', {
    fun: 'merge',
    id: id,
  })
}

const delOneFUn = (id) => {
  emit('getId', {
    fun: 'del',
    id: id,
  })
}

const addChildFun = (id) => {
  emit('getId', {
    fun: 'addChild',
    id: id,
  })
}

const addBrotherFun = (id) => {
  emit('getId', {
    fun: 'addBrother',
    id: id,
  })
}

</script>

<template>
  <div class="tree_view">
    <div class="one_tree_node">
      <el-popover
          placement="bottom"
          :width="200"
          trigger="click"
      >
        <template #reference>
          <div class="tree_node">{{ props.treeData.label }}</div>
        </template>
        <div class="navList">
          <div class="oneNav" @click="delOneFUn(props.treeData.id)">撤销部门/工作组</div>
          <div class="oneNav" @click="addChildFun(props.treeData.id)">添加下级部门/工作组</div>
<!--          <div class="oneNav" @click="addBrotherFun(props.treeData.id)">添加节点</div>-->
          <div class="oneNav" @click="mergeFun(props.treeData.id)">合并部门/工作组</div>
        </div>
      </el-popover>
      <template v-if="props.treeData.child.length !== 0">
        <div class="verticalLine_body">
          <div class="verticalLine"></div>
        </div>
        <div class="tree_node_btn" >
          <el-icon class="btn_icon" v-show="showChild" @click="showOrHidChild">
            <Plus />
          </el-icon>
          <el-icon class="btn_icon" v-show="!showChild" @click="showOrHidChild">
            <Minus />
          </el-icon>
        </div>
        <div v-show="!showChild">
          <treeChildren @getId="getIdFun"  :tree-data="props.treeData.child"></treeChildren>
        </div>
      </template>
    </div>


  </div>
</template>

<style scoped lang="scss">
.tree_view {
  width: max-content;
  height: auto;
  position: relative;

  .one_tree_node {
    flex-wrap: wrap;
    display: flex;
    display: -webkit-flex;
    display: -moz-flex;
    align-items: center;
    justify-content: center;
    width: max-content;
    height: auto;

    .tree_node {
      width: max-content;
      height: auto;
      font-size: 16px;
      box-sizing: border-box;
      padding: 10px 20px;
      cursor: pointer;
      border: 1px solid #939090;
      box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.1);
    }

    .verticalLine_body {
      width: 100%;
      height: auto;
      display: flex;
      display: -webkit-flex;
      display: -moz-flex;
      align-items: center;
      justify-content: center;

      .verticalLine {
        width: 1px;
        flex-basis: 1px;
        flex-shrink: 0;
        height: 30px;
        background-color: #939090;
      }
    }

    .tree_node_btn {
      width: 100%;
      height: auto;
      display: flex;
      display: -webkit-flex;
      display: -moz-flex;
      align-items: center;
      position: relative;
      justify-content: center;

      .btn_icon {
        cursor: pointer;
        font-size: 24px;
        position: relative;
        z-index: 1000;
        border: 1px solid deepskyblue;
        box-sizing: border-box;
        background-color: #ffffff;
        color: deepskyblue;
      }
    }
  }
}

.navList {
  width: 100%;
  height: auto;

  .oneNav {
    width: 100%;
    height: 40px;
    cursor: pointer;
    box-sizing: border-box;
    padding-left: 10px;
    line-height: 40px;
    border-bottom: 1px solid #eeeeee;
    background-color: #ffffff;
    transition: background-color 0.5s;
    -webkit-transition: background-color 0.5s;

    &:last-child {
      border-bottom: none;
    }

    &:hover {
      background-color: #eeeeee;
    }
  }
}
</style>